<template>
  <div class="login">
    <div class="cover">
      <img :src="about" class="img1">
      <img :src="title" class="img2">
    </div>
    <div class="header-contant">
      <div class="tips">
        <div class="t1">输入手机号码</div>
        <div class="t2">登陆注册</div>
      </div>
    </div>
    <div class="main-contant">
      <div class="input_view">
        <input
          type="number"
          v-model="mobile"
          oninput="if(value.length>11)value=value.slice(0,11)"
          id
          class="input"
          @blur="inputBack()"
        >
        <img :src="x" class="img" v-if="mobile.length>0" @click="clean">
      </div>
      <div class="input_view">
        <input
          type="number"
          v-model="inputCode"
          oninput="if(value.length>4)value=value.slice(0,4)"
          @blur="inputBack()"
          class="input"
        >
        <span class="send_btn" @click="get" v-if="status==0">发送验证码</span>
        <span class="send_btn" v-if="status == 1">
          <yd-countdown :time="59" timetype="second" format="{%s}秒" :callback="test"></yd-countdown>
        </span>
      </div>
      <div class="next" @click="jumpto('/')">下一步</div>
    </div>
  </div>
</template>

<style lang="less" scoped>
@import "index.less";
</style>

<script>
import { mapActions } from "vuex";
export default {
  name: "login",
  data() {
    return {
      head_bg: require("../../assets/login/bgc.png"),
      inputCode: "",
      code: "",
      mobile: "",
      status: 0,
      about: require("../../assets/about/logo.png"),
      title: require("../../assets/about/title.png"),
      x: require("../../assets/x.png")
    };
  },
  beforeCreate() {
    const openid = (this.$route.query && this.$route.query["openid"]) || "";
    if (!openid) {
      window.location.href =
        window.sysinfo.AUTH_URL +
        "&url=" +
        encodeURIComponent(window.location.href);
    }
  },
  created() {
    this.change(false);
  },
  methods: {
    ...mapActions("Home", ["change"]),
    test() {
      this.status = 0;
      this.$forceUpdate();
    },
    jumpto(path) {
      // 注册用户
      this.$dialog.loading.open("正在登陆");
      this.$http
        .post("/register/Check", {
          mobile: this.mobile,
          code: this.inputCode
        })
        .then(res => {
          if (res.data.errno == 0) {
            this.$dialog.loading.close();
            this.$router.push({ path: path });
          } else {
            this.$dialog.toast({
              mes: "验证码错误",
              timeout: 1500,
              icon: "error"
            });
          }
        });
    },
    clean() {
      this.mobile = "";
    },
    get() {
      // 获取验证码
      this.status = 1;
      this.$dialog.loading.open("正在加载");
      console.log(this.mobile);
      this.$http
        .post("/verity/Code", {
          mobile: this.mobile
        })
        .then(res => {
          if (res.data.errno == 0) {
            console.log("成功");
            // this.code = res.data.data.code;
            this.$dialog.toast({
              mes: "发送成功",
              timeout: 1500,
              icon: "success"
            });
          } else {
            this.$dialog.toast({
              mes: "发送失败",
              timeout: 1500,
              icon: "error"
            });
          }
          this.$dialog.loading.close();
        });
    },
    // 注册用户
    userRegister(path) {
      this.$dialog.loading.open("正在登陆");
      this.$http.post("/user/Register", { mobile: this.mobile }).then(res => {
        if (res.data.errno == 0) {
          this.$dialog.loading.close();
          this.$router.push({ path: path });
        }
      });
    }
  },
  components: {}
};
</script>
